/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import controllers.CalendarManager;
import controllers.PlaceManager;
import entities.City;
import entities.Event;
import entities.Nation;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class UpdateEventBean {
    
    @EJB
    private CalendarManager cm;
    
    @EJB
    private PlaceManager pm;
    
    private Event event;
    
    @NotNull(message = "Select a country")
    private String country; 
    
    @NotNull(message = "Select a city")
    private String city; 
    
    private Map<String,Map<String,String>> data = new HashMap<String, Map<String,String>>();
    private Map<String,String> countries;
    private Map<String,String> cities;

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
    
    public String getCountry() {
        return country;
    }
 
    public void setCountry(String country) {
        this.country = country;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
 
    public Map<String, String> getCountries() {
        return countries;
    }
 
    public Map<String, String> getCities() {
        return cities;
    }
    
    public UpdateEventBean () {
        this.event = (Event) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedevent");
    }
    
    @PostConstruct
    public void init() {
        countries  = new HashMap<String, String>();
        
        Map<String,String> map = null;
        ArrayList<Nation> lnations = new ArrayList<>();
        ArrayList<City> lcities = new ArrayList<>();
        
        lnations.addAll(pm.getAllNations());
        
        Iterator<Nation> iternation = lnations.iterator();
        Nation n = null;
        while (iternation.hasNext()) {
            n = iternation.next();
            countries.put(n.getName(), n.getName());
            map = new HashMap<String, String>();
            lcities = new ArrayList<>();
            lcities.addAll(pm.getCitiesByNation(n));
            Iterator<City> itercity = lcities.iterator();
            City c = null;
            while (itercity.hasNext()) {
                c = itercity.next();
                map.put(c.getName(), c.getName());
            }
            data.put(n.getName(), map);
        }
        
        country = event.getPlace().getCity().getNation().getName();
        cities = data.get(country);
        city = event.getPlace().getCity().getName();
    }
    
    public void onCountryChange() {
        if(country !=null && !country.equals(""))
            cities = data.get(country);
        else
            cities = new HashMap<String, String>();
    }
    
    public void validateDate (FacesContext context, UIComponent toValidate, Object value) {
        Date newdate = (Date) value;
        GregorianCalendar newcal = new GregorianCalendar();
        newcal.setTime(newdate);
        GregorianCalendar todaycal = new GregorianCalendar();
        if (newcal.compareTo(todaycal) <= 0) {
            ((UIInput) toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Date must in the future.");
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void validateEndTime (FacesContext context, UIComponent toValidate, Object value) {
        Date endtime = (Date) value;
        Date starttime = (Date) ((UIInput) context.getViewRoot().findComponent("formid:startime")).getValue();
        if (starttime!=null && endtime!=null) {
            if (endtime.getHours() < starttime.getHours()) {
                ((UIInput) toValidate).setValid(false);
                FacesMessage message = new FacesMessage("Time inconsistent");
                context.addMessage(toValidate.getClientId(context), message);
            } else if (endtime.getHours() == starttime.getHours() && endtime.getMinutes() < starttime.getMinutes()) {
                 ((UIInput) toValidate).setValid(false);
                FacesMessage message = new FacesMessage("Time inconsistent");
                context.addMessage(toValidate.getClientId(context), message);
            }
        }
    }
    
    public String save () {
        pm.modifyPlace(event.getPlace(), pm.getCity(city, pm.getNation(country)));
        cm.modifyEvent(event);
        return "calendar?faces-redirect=true";
    }
    
}
